/*
 * TCSS 360 - Autumn 2011
 * Group2: ManuscriptPR
 *
 * SideBarPanel class
 * Author: Christopher Yeaman
 * Author: Tony Butterfield
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import user.Conference;
import user.User;

/**
 * SideBar panel contains drop down lists for Conference User and Role.
 * 
 * @author Christopher Yeaman
 * @author Tony Butterfield
 * @version 12/1/2011
 */
@SuppressWarnings("serial")
public class SideBarPanel extends JPanel
{
  //STATIC FIELDS ****************************************
  /** default width of the sidebar Panel. */
  public static final int WIDTH = 125;

  /** default height of the sidebar panel. */
  public static final int HEIGHT = 300;
  
  /** rows in layout.  */
  public static final int ROWS = 6;
  
  
  // INSTANCE FIELDS **************************************
  /** reference to the ManuscriptFrame that contains this panel. */
  private MainFrame1 my_parent;
  
  /** drop-down combo box for conferences.  */
  private JComboBox my_conf_ddl;
  
  /** drop-down combo box for users.  */
  private JComboBox my_user_ddl;
  
  /** program chair button. */
  private JButton my_pc_button;
  
  /** reviewer button. */
  private JButton my_re_button;
  
  /** author button. */
  private JButton my_au_button;

  // CONSTRUCTOR ******************************************
  /**
   * constructs an instance of the SideBarPanel.
   * @param the_parent ManuscriptFrame that contains this panel
   */
  public SideBarPanel(final MainFrame1 the_parent)
  {
    super(new BorderLayout());
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setBorder(BorderFactory.
              createTitledBorder(BorderFactory.
                                 createEtchedBorder(EtchedBorder.LOWERED), 
                  "Select"));    
    my_parent = the_parent;    
    setup();
  }

  /**
   * add gui elements to panel.
   * @author Christopher Yeaman; reimplementation by Tony Butterfield
   */
  void refresh()
  {
    if (my_parent.getCurrentUser().getRole(my_parent.getCurrentConference()) != null)
    {
      switch (my_parent.getCurrentUser().getRole(my_parent.getCurrentConference()))
      {
        case PROGRAM_CHAIR:
          my_pc_button.setEnabled(true);
          my_re_button.setEnabled(true);
          my_au_button.setEnabled(true);
          break;
        case REVIEWER:
          my_pc_button.setEnabled(false);
          my_re_button.setEnabled(true);
          my_au_button.setEnabled(true);
          break;
        case AUTHOR:
          my_pc_button.setEnabled(false);
          my_re_button.setEnabled(false);
          my_au_button.setEnabled(true);
          break;
        default:
          break;
      }      
    }
    else
    {
      JOptionPane.showMessageDialog(this,"Booger!  The user isn't coming up an AUTHOR!");
    }
  }

  /**
   * setup gui elements within this panel.
   * 
   * @author Christopher Yeaman and Tony Butterfield
   */
  private void setup()
  {
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    
    final JLabel label1 = new JLabel("Conference");    
    label1.setHorizontalAlignment(SwingConstants.LEFT);       
    
    final JLabel label2 = new JLabel("User");
    label2.setHorizontalAlignment(SwingConstants.LEFT);
    
    final JLabel label3 = new JLabel("Role");
    label2.setHorizontalAlignment(SwingConstants.LEFT);
    
    final List<String> all_conf_names = my_parent.getUserCollection().getAllConferenceNames();
    final List<String> all_user_names = my_parent.getUserCollection().getAllUserNames();
    
    my_conf_ddl = new JComboBox();
    for (String s : all_conf_names)
    {
      my_conf_ddl.addItem(my_parent.getUserCollection().getConference(s));
    }
    my_conf_ddl.setSelectedItem(my_parent.getCurrentConference());
    
    my_user_ddl = new JComboBox();
    for (String s : all_user_names)
    {
      my_user_ddl.addItem(my_parent.getUserCollection().getUser(s));
    }
    
    my_pc_button = new JButton("Program Chair");
    my_pc_button.setEnabled(false);
    
    my_re_button = new JButton("Reviewer");
    my_re_button.setEnabled(false);
    
    my_au_button = new JButton("Author");
    my_au_button.setEnabled(false);
    
    addActions();

    final JPanel control_panel = new JPanel(new GridLayout(0, 1));
    
    control_panel.add(label1);
    control_panel.add(my_conf_ddl);
    control_panel.add(label2);
    control_panel.add(my_user_ddl);
    control_panel.add(label3);
    control_panel.add(my_pc_button);
    control_panel.add(my_re_button);
    control_panel.add(my_au_button);
    
    add(control_panel, BorderLayout.NORTH);
    refresh();
  }

  /** 
   * Adds actionListeners to controls.
   * 
   * @author Tony Butterfield <b>Editing by: </b>Christopher Yeaman
   */
  private void addActions()
  {
    my_pc_button.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(final ActionEvent the_e)
      {
        my_parent.remove(my_parent.my_panel2);
        my_parent.my_panel1 = new PCViewsPanel2(my_parent);
        my_parent.add(my_parent.my_panel1);
        my_parent.refresh();
             
      }      
      
    });
    
    my_re_button.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(final ActionEvent the_e)
      {
        //TODO:  USER story not yet implemented
        my_parent.remove(my_parent.my_panel1);
        my_parent.remove(my_parent.my_panel2);
        my_parent.refresh();
        
        JOptionPane.showMessageDialog(my_parent, 
                                      "The 'Reviewer Submits' Story not yet implemented.");
        
      }      
    });
    
    my_au_button.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(final ActionEvent the_e)
      {
        my_parent.remove(my_parent.my_panel1);
        my_parent.remove(my_parent.my_panel2);        
        my_parent.refresh();
        //TODO:  USER story not yet implemented        
        JOptionPane.showMessageDialog(my_parent, 
                                      "The 'Author Submits' Story not yet implemented.");

      }      
    });
    
    my_conf_ddl.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(final ActionEvent the_e)
      {
        my_parent.remove(my_parent.my_panel1);
        my_parent.remove(my_parent.my_panel2);
        my_parent.setCurrentConference((Conference) my_conf_ddl.getSelectedItem());
        my_parent.refresh();
        my_parent.pack();
        refresh();
      }
    });
    
    my_user_ddl.addActionListener(new ActionListener()
    {      
      @Override
      public void actionPerformed(final ActionEvent the_e)
      {
        my_parent.remove(my_parent.my_panel1);
        my_parent.remove(my_parent.my_panel2);
        my_parent.setCurrentUser((User) my_user_ddl.getSelectedItem());
        my_parent.refresh();
        my_parent.pack();
        refresh();
      }
    });
    
  }

}
